/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package flight.model;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import middleware.MutableFloat;
import middleware.MutableInteger;
import middleware.MutableLong;
import middleware.MutableString;
import middleware.Serializable;
import middleware.Serialization;

/**
 *
 * @author tam
 */
public class Flight implements Serializable{
    public int id;
    public String source;
    public String destination;
    public long time;
    public float airfare;
    public int noOfSeats;

    public Flight(){
        
    }

    @Override
    public String toString(){
        Date d = new Date(time);
        DateFormat f = new SimpleDateFormat("dd/MM/yy hh:mm");
        
        return "flight " + id + " from " + source + " to " + destination + " at " + airfare + " SGD " + f.format(d);
    }

    public int serialize(byte[] buffer, int offset) {
       
        offset = Serialization.putInt(id, buffer, offset);
        offset = Serialization.putString(source, buffer, offset);
        offset = Serialization.putString(destination, buffer, offset);
        offset = Serialization.putLong(time, buffer, offset);
        offset = Serialization.putFloat(airfare, buffer, offset);
        offset = Serialization.putInt(noOfSeats, buffer, offset);
        
        return offset;
    }

    public Serializable duplicate() {
        Flight f = new Flight();
        return f;
    }

    public String getSerializeName() {
        //throw new UnsupportedOperationException("Not supported yet.");
        return "Flight";
    }

    public int deserialize(byte[] buffer, int offset) {
        //throw new UnsupportedOperationException("Not supported yet.");
        MutableInteger dropbox = new MutableInteger();
        offset = Serialization.getInt(dropbox, buffer, offset);
        id = dropbox.getValue();
        MutableString str = new MutableString();
        offset = Serialization.getString(str, buffer, offset);
        source = str.getValue();
        offset = Serialization.getString(str, buffer, offset);
        destination = str.getValue();
        MutableLong l = new MutableLong();
        offset = Serialization.getLong(l, buffer, offset);
        time = l.getValue();
        MutableFloat mf = new MutableFloat();
        offset = Serialization.getFloat(mf, buffer, offset);
        airfare = mf.getValue();
        offset = Serialization.getInt(dropbox, buffer, offset);
        noOfSeats = dropbox.getValue();
        return offset;
    }
}
